package com.fastloan.mgt.application.contract.service.impl;

import com.fastloan.mgt.application.base.model.Application;
import com.fastloan.mgt.application.base.service.ApplicationService;
import com.fastloan.mgt.application.base.util.ApplicationUtil;
import com.fastloan.mgt.application.contract.service.ChoiceContractService;
import com.fastloan.mgt.application.contract.service.impl.contractmode.ContractMode;
import com.fastloan.mgt.processcontrol.base.util.BeanFactoryUtil;
import com.fastloan.mgt.product.basics.model.Product;
import com.fastloan.mgt.product.basics.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Optional;

import static com.fastloan.mgt.product.enums.ProductType.ADVANCE;

/**
 * @author yanwe
 * createTime 2017-12-2017/12/20 16:50
 */
@Service
public class ChoiceContractServiceImpl implements ChoiceContractService {

    private static final Logger logger = LoggerFactory.getLogger(ChoiceContractServiceImpl.class);

    @Autowired
    private ProductService productService;

    @Autowired
    private ApplicationService applicationService;

    @Override
    public void choiceContract(String applicationId) {
        if (applicationId == null) {
            logger.error("进件预签约失败！进件ID不存在");
            return;
        }
        Application application = applicationService.selectByPrimaryKey(applicationId);
        if (application == null) {
            logger.error("进件预签约失败！进件：" + applicationId + "不存在");
            return;
        }
        Product product = productService.selectByPrimaryKey(application.getProductId());
        if (product == null) {
            logger.error("进件预签约失败！进件ID:" + applicationId + "产品：" + application.getProductId() + "不存在");
            return;
        }
        if (product.getProductType().equals(ADVANCE.getType())) {
            logger.info("极鲜贷产品！进件ID:" + applicationId + "产品：" + application.getProductId());
            return;
        }
        Map<String, ContractMode> contractModeMap = BeanFactoryUtil.getBeansOfType(ContractMode.class);
        Optional<ContractMode> contractModeOptional = contractModeMap.values().stream()
                .filter(contractMode -> contractMode.contractType().equals(product.getProductType()))
                .findFirst();
        if (!contractModeOptional.isPresent()) {
            throw new RuntimeException("未知的产品类型:" + product.getProductType() + "无法找到对应签约模式！");
        }
        contractModeOptional.get().contract(application, product);
        //经营类此值只是建议额度（在PC签约时显示，签约时改为最终实际额度），消费类此值为最终额度
        Application finalApplication = applicationService.selectByPrimaryKey(applicationId);
        finalApplication.setFinalAmount(ApplicationUtil.getFinalAmount(finalApplication.getApplicationId()
                , finalApplication.getAuditAmount(), finalApplication.getApplyAmount()));
        applicationService.updateByPrimaryKeySelective(finalApplication);
    }
}
